package com.starblink.imgsearch.result.ui.widget

import android.graphics.Color
import android.text.TextUtils
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.TextView
import androidx.core.graphics.ColorUtils
import com.starblink.android.basic.ext.toastShowDev
import com.starblink.android.basic.ext.toastShowDev1

/**
 *  Create by yuanl at 2023/5/31
 **/
class SearchHintAdapter(private val data: List<String>) : BaseAdapter() {

    override fun getCount(): Int {
        return data.size
    }

    override fun getItem(position: Int): String {
        return data[position]
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        val view: View
        val viewHolder: ViewHolder
        if (convertView == null) {
            view = TextView(parent.context).apply {
                textSize = 14f
                setTextColor(ColorUtils.setAlphaComponent(Color.WHITE, 128))
                maxLines = 1
                gravity = Gravity.CENTER_VERTICAL
                ellipsize = TextUtils.TruncateAt.END
            }
            viewHolder = ViewHolder(view)
            view.tag = viewHolder
        } else {
            view = convertView
            viewHolder = convertView.tag as ViewHolder
        }
        try {
            viewHolder.view.text = data[position]
        } catch (e: Exception) {
            viewHolder.view.text = ""
            toastShowDev1("数据越界")
        }

        return view
    }

    inner class ViewHolder(val view: TextView)

}